﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoulHackerCUnite.Data;
using System.Data.SQLite;

namespace SoulHackerCUnite.Converters
{
    public static class TranslateDatas
    {
        public static List<Demon> TranslateData2Demons(SQLiteDataReader reader)
        {
            List<Demon> demons = new List<Demon>();
            if(reader!=null)
            {
                while (reader.Read())
                {
                    Demon demon = new Demon();
                    demon.Name = reader["[Name]"].ToString();
                    demon.RaceJ = reader["[race_j]"].ToString();
                    demon.Level = (int)(long)reader["[Level]"];
                    demon.UnitePrec = (int)reader["[UnitePrec]"];
                    demon.JName = reader["[jname]"].ToString();
                    demon.Alias = reader["[Alias]"].ToString();

                    demon.HeathPoint = (int)(long)reader["[HP]"];
                    demon.ManaPoint = (int)(long)reader["[MP]"];

                    demon.Strength = (int)(long)reader["[Strength]"];
                    demon.Intelligence = (int)(long)reader["[Intelligence]"];
                    demon.Magic = (int)(long)reader["[Magic]"];
                    demon.Stamina = (int)(long)reader["[Stamina]"];
                    demon.Agility = (int)(long)reader["[Agility]"];
                    demon.Luck = (int)(long)reader["[Luck]"];

                    demon.Skill1 = reader["[Skill1]"].ToString();
                    demon.Skill2 = reader["[Skill2]"].ToString();
                    demon.Skill3 = reader["[Skill3]"].ToString();
                    demon.Skill4 = reader["[Skill4]"].ToString();
                    demon.Skill5 = reader["[Skill5]"].ToString();
                    demon.Skill6 = reader["[Skill6]"].ToString();

                    demon.Disposition = reader["[Disposition]"].ToString();
                    demon.InhertType = reader["[Inherit]"].ToString();
                    //todo: add all fields
                    demons.Add(demon);
                }
            }
            return demons;
        }

        public static int ConvertData2Int(Object obj)
        {
            int result = 0;
            if (obj is Int32)
                result = (int)obj;
            else if (obj is Int64)
                result = (int)(long)obj;

            return result;
        }

        public static List<UniteRule> TranslateData2UniteRules(SQLiteDataReader reader)
        {
            List<UniteRule> rules = new List<UniteRule>();
            if (reader != null)
            {
                while (reader.Read())
                {
                    UniteRule rule = new UniteRule();
                    rule.Race1 = reader["race1"].ToString();
                    rule.Race2 = reader["race2"].ToString();
                    rule.Result = reader["result"].ToString();
                    rule.Type = reader["type"].ToString();

                    rules.Add(rule);
                }
            }
            return rules;
        }

        public static List<Race> TranslateData2Race(SQLiteDataReader reader)
        {
            List<Race> races = new List<Race>();
            if (reader != null)
            {
                while (reader.Read())
                {
                    Race race = new Race();
                    race.RaceName = reader["Race"].ToString();
                    race.GreatRaceName = reader["GreatRace"].ToString();
                    race.UnitePrec = (int)reader["UnitePrec"];                    

                    races.Add(race);
                }
            }
            return races;
        }

        public static List<Skill> TranslateData2Skill(SQLiteDataReader reader)
        {
            List<Skill> skills = new List<Skill>();
            if (reader != null)
            {
                while (reader.Read())
                {
                    Skill skill = new Skill();
                    skill.CName = reader["[Name]"].ToString();
                    skill.JName = reader["[JName]"].ToString();
                    skill.Power = ConvertData2Int(reader["[Power]"]);
                    skill.IsMagic = ConvertData2Int(reader["[MP]"])>0;

                    skills.Add(skill);
                }
            }
            return skills;
        }

        public static List<SkillInhert> TranslateData2SkillInhert(SQLiteDataReader reader)
        {
            List<SkillInhert> skills = new List<SkillInhert>();
            if (reader != null)
            {
                while (reader.Read())
                {
                    SkillInhert skill = new SkillInhert();
                    skill.SkillJName = reader["magic"].ToString();
                    skill.InhertType = reader["type"].ToString();
                    skill.Rank = ConvertData2Int(reader["rank"]);

                    skills.Add(skill);
                }
            }
            return skills;
        }
    }
}
